package com.bcu.wish.controller;

import com.bcu.wish.config.GlobalConfig;
import com.bcu.wish.entity.Message;
import com.bcu.wish.entity.Word;
import com.bcu.wish.service.WordService;
import com.bcu.wish.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Api(tags = "考场信息模块")
@RestController
@RequestMapping("/api/word")
public class WordController {

    @Resource
    private WordService wordService;

    @Resource
    private GlobalConfig globalConfig;

    @ApiOperation(value = "上传Word")
    @PostMapping(value = "/load",headers = "content-type=multipart/form-data")
    public Message uploadWord(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        String fileName = UploadUtil.upload(globalConfig.filePath,files, request);
        Word word = new Word();
        word.setWordId(1);
        word.setWordUrl(globalConfig.hostUrl+"resource/"+fileName);
        return Message.success(null).add(wordService.insert(word));

    }

    @ApiOperation(value = "更新Word")
    @PostMapping(value = "/update",headers = "content-type=multipart/form-data")
    public Message updateWord(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request) throws IOException {
        String fileName = UploadUtil.upload(globalConfig.filePath,files, request);
        Word word = new Word();
        word.setWordId(1);
        word.setWordUrl(globalConfig.hostUrl+"resource/"+fileName);
        wordService.updateByPrimaryKey(word);
        return Message.success(null).add(word.getWordUrl());

    }

    @ApiOperation(value = "获取URL")
    @GetMapping("/")
    public Message getUrl() {
        return Message.success(null).add(wordService.selectByPrimaryKey(1));
    }
}
